<template>
	<!-- 收货地址 -->
	<view class="address flex-row border-radius" style="background-color: #fff;width:100%;height:150rpx"
		v-for="(item,index) in addresses.list" @click="selectAddress(index)" :key="index">
		<view class="flex-row-ct theme-color margin-lr-com iconfont icon-dingwei1" style="font-size: 25px"></view>
		<view class="flex-column-st">
			<view class="message" type="text">{{item.address}}</view>
			<view class="name" type="text">{{item.receiver}} {{item.phone}}</view>
		</view>
		<view class="flex-column-st theme-color margin-lr-com" style="font-size: 20px;margin-left: auto;">
			<view class="tag-view" v-if="item.defaultFlag===1">
				<uni-tag text="默认" type="error" size="small" />
			</view>
			<view class=" theme-color iconfont icon-bianjiputong" style="font-size: 20px;margin-left: auto;"
				@click.stop="edit(item)"></view>
		</view>
	</view>
	<!-- 底部导航栏 -->
	<view class="" style="height:80px;"></view>
	<view class="tabbar flex-row-st "
		style="position: fixed; bottom: 0; left: 0; right: 0;width:100%;height:60px;background-color: #fff;">
		<view class="flex-row-ct theme-bg-color margin-lr-com" style="color: #fff;
		width: 100%;height: 70rpx;border-radius: 10rpx;" @click="addRess">+添加收货地址</view>
	</view>
</template>

<script lang="ts" setup>
	import { reactive, ref } from 'vue';
	import { onShow, onPullDownRefresh } from "@dcloudio/uni-app"
	import { deliveryAddress } from '../../api/shop/shop';
	// const setDefaultAddress=(addressId)=> {
	//     addresses.forEach(address => {
	//       if (address.id === addressId) {
	//         address.isDefault = true;
	//       } else {
	//         address.isDefault = false;
	//       }
	//     });
	// 如果需要，这里还可以添加代码以持久化存储更改后的地址列表
	// }
	//
	onShow(() => {
		gitdeliveryAddress()
	})
	onPullDownRefresh(() => {
		console.log("下拉刷新")
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 1000);
	})
	// 获取list
	const gitdeliveryAddress = () => {
		deliveryAddress().then(res => {
			console.log(res.data)
			addresses.list = res.data
		})
	}
	//编辑
	const edit = (e) => {
		// uni.showToast({
		// 			title: '功能正在开发中...',
		// 			icon: 'none',
		// 			duration: 2000
		// 		});
		e = JSON.stringify(e)
		console.log(e)
		uni.navigateTo({
			url: '/pages/shop/editAddress?item=' + e
		})
	}

	//添加收货地址
	const addRess = () => {
		// uni.showToast({
		// 			title: '功能正在开发中...',
		// 			icon: 'none',
		// 			duration: 2000
		// 		});
		uni.navigateTo({
			url: '/pages/shop/deliveryAddress'
		})
	}
	//收货地址列表
	const addresses = reactive({
		list: []
	})
	// 选择地址
	const selectAddress = (index) => {
		const address = JSON.stringify(addresses.list[index])
		// // 1. 获取当前页面栈实例（此时最后一个元素为当前页）
		// let pages = getCurrentPages()

		// // 2. 上一页面实例
		// // 注意是length长度，所以要想得到上一页面的实例需要 -2
		// // 若要返回上上页面的实例就 -3，以此类推
		// let prevPage = pages[pages.length - 2]

		// // 3. 给上一页面实例绑定getValue()方法和参数（注意是$vm）
		// prevPage.getAddress(address)

		// // 4. 返回上一页面
		uni.navigateBack({
			delta: 1 // 返回的页面数
		})
		uni.setStorageSync('address', address)
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #fff;
	}

	.tabbar {
		box-shadow: $uni-shadow-lg;
	}
</style>